from typing import List


class Solution:
    def findMaxLength(self, nums: List[int]) -> int:
        ans = 0
        count = [0, 0]
        hashset = {0: 0}
        for i, num in enumerate(nums):
            count[num] += 1
            differ = count[1] - count[0]
            if differ in hashset:
                ans = max(ans, i + 1 - hashset[differ])
            else:
                hashset[differ] = i + 1
        return ans


if __name__ == "__main__":
    print(Solution().findMaxLength(nums=[0, 1]))  # 2
    print(Solution().findMaxLength(nums=[0, 1, 0]))  # 2
